'use strict';

/**
 * @ngdoc overview
 * @name webApp
 * @description
 * # webApp
 *
 * Main module of the application.
 */
angular.module('webApp',
  [
    'ionic',
    'angular-cache',
    'ngSanitize',
    'restangular',
    'pasvaz.bindonce',
    'ngImgCrop',
    'ngIOS9UIWebViewPatch',
    'ngTouch',
    'ngStorage'
  ]);

//配置变量服务
angular.module('webApp').constant('client_id','0IiNj6B4hJui25Ih');
angular.module('webApp').constant('client_secret','JhhvEXQmLRcLWzYpTODYGpJ7tWhanp3b');
angular.module('webApp').constant('grant_type','password');
angular.module('webApp').constant('ACCESS_LEVEL',{'signIn': 2, 'signOut': 1, 'public': 0});

angular.module('webApp')
  .config(function($stateProvider,$urlRouterProvider,$httpProvider,ACCESS_LEVEL){
  //重定向
  $urlRouterProvider.when('','/root/home');
  $urlRouterProvider.otherwise(function(){
    alert('网页安全警告！');
  });
  //拦截器
  $httpProvider.interceptors.push('httpIntercept');
  $stateProvider
    //虚拟模板
    .state('root', {
      url: '/root',
      abstract: true,
      templateUrl: 'views/nav-box.html',
      ACCESS_LEVEL: ACCESS_LEVEL.public
    })
    //主页
    .state('root.home', {
      url: '/home',
      views: {
        'content' :{
          templateUrl: 'views/homeContent.html',
          controller: 'homeContent'
        },
        'footer': {
          templateUrl: 'views/footer.html',
          controller: 'footer'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.public,
      onEnter: function($rootScope){
        $rootScope.footerShow = true;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
        $rootScope.routeHide = true;
      }
    })
    //服务
    .state('root.server', {
      url: '/server',
      views: {
        'content' :{
          templateUrl: 'views/serverContent.html',
          controller: 'serverContent'
        },
        'footer': {
          templateUrl: 'views/footer.html',
          controller: 'footer'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.public,
      onEnter: function($rootScope){
        $rootScope.footerShow = true;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
        $rootScope.routeHide = true;
      }
    })
    //报告
    .state('root.report', {
      url: '/report',
      views: {
        'content' :{
          templateUrl: 'views/reportContent.html',
          controller: 'reportContent'
        },
        'footer': {
          templateUrl: 'views/footer.html',
          controller: 'footer'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.public,
      onEnter: function($rootScope){
        $rootScope.footerShow = true;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
        $rootScope.routeHide = true;
      }
    })
    //更多
    .state('root.more', {
      url: '/moreContent',
      views: {
        'content' :{
          templateUrl: 'views/morecontent.html',
          controller: 'moreContent'
        },
        'footer': {
          templateUrl: 'views/footer.html',
          controller: 'footer'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.public,
      onEnter: function($rootScope){
        $rootScope.footerShow = true;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
        $rootScope.routeHide = true;
      }
    })
    //用户
    .state('root.userTable', {
      url: '/userTable',
      views: {
        'content' :{
          templateUrl: 'views/usertable.html',
          controller: 'userTable'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.public,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //登录
    .state('root.signIn', {
      url: '/signIn',
      views: {
        'content' :{
          templateUrl: 'views/signin.html',
          controller: 'signIn'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.signOut,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //注册
    .state('root.registered', {
      url: '/registered',
      views: {
        'content' :{
          templateUrl: 'views/registered.html',
          controller: 'registered'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.signOut,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //搜索
    .state('root.search', {
      url: '/search',
      views: {
        'content' :{
          templateUrl: 'views/search.html',
          controller: 'search'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.public,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //设置
    .state('root.setUp', {
      url: '/setUp',
      views: {
        'content' :{
          templateUrl: 'views/setup.html',
          controller: 'setUp'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.public,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //团队
    .state('root.team', {
      url: '/team',
      views: {
        'content' :{
          templateUrl: 'views/team.html',
          controller: 'team'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.public,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //社区
    .state('root.community', {
      url: '/community',
      views: {
        'content' :{
          templateUrl: 'views/community.html',
          controller: 'community'
        },
        'footer': {
          templateUrl: 'views/footer.html',
          controller: 'footer'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.public,
      onEnter: function($rootScope){
        $rootScope.footerShow = true;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
        $rootScope.routeHide = true;
        $rootScope.routeHide = false;
      }
    })
    //正文
    .state('root.mainText', {
      url: '/mainText',
      views: {
        'content' :{
          templateUrl: 'views/mainText.html',
          controller: 'mainText'
        },
        'footer': {
          templateUrl: 'views/footer.html',
          controller: 'footer'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.public,
      onEnter: function($rootScope){
        $rootScope.footerShow = true;
        $rootScope.rightBut = 'commBut';
        $rootScope.rightButShow = true;
        $rootScope.mobShow = true;
        $rootScope.routeHide = true;
      }
    })
    //正文评论
    .state('root.textCommunicate', {
      url: '/textCommunicate',
      views: {
        'content' :{
          templateUrl: 'views/textcommunicate.html',
          controller: 'textCommunicate'
        },
        'footer': {
          templateUrl: 'views/footer.html',
          controller: 'footer'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.public,
      onEnter: function($rootScope){
        $rootScope.footerShow = true;
        $rootScope.rightBut = 'write';
        $rootScope.rightButShow = true;
        $rootScope.mobShow = false;
        $rootScope.routeHide = false;
      }
    })
    //产品
    .state('root.product', {
      url: '/product',
      views: {
        'content' :{
          templateUrl: 'views/product.html',
          controller: 'product'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.public,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //用户信息
    .state('root.userData', {
      url: '/userData',
      views: {
        'content' :{
          templateUrl: 'views/userdata.html',
          controller: 'userData'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.signIn,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //用户评论
    .state('root.userCommunicate', {
      url: '/userCommunicate',
      views: {
        'content' :{
          templateUrl: 'views/usercommunicate.html',
          controller: 'userCommunicate'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.signIn,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //用户收藏
    .state('root.userCollection', {
      url: '/userCollection',
      views: {
        'content' :{
          templateUrl: 'views/userCollection.html',
          controller: 'userCollection'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.signIn,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //用户消息
    .state('root.userInformation', {
      url: '/userInformation',
      views: {
        'content' :{
          templateUrl: 'views/userinformation.html',
          controller: 'userInformation'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.signIn,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //修改用户信息
    .state('root.modifyInformation', {
      url: '/modifyInformation',
      views: {
        'content' :{
          templateUrl: 'views/modifyinformation.html',
          controller: 'modifyinformation'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.signIn,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //关于我们
    .state('root.aboutUs', {
      url: '/aboutUs',
      views: {
        'content' :{
          templateUrl: 'views/aboutus.html',
          controller: 'aboutUs'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.public,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //截图
    .state('root.imgCrop', {
      url: '/imgCrop',
      views: {
        'content' :{
          templateUrl: 'views/imgcrop.html',
          controller: 'ImgcropCtrl',
          controllerAs: 'imgCrop'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.signIn,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //忘记密码
    .state('root.forgetPassword', {
      url: '/forgetPassword',
      views: {
        'content' :{
          templateUrl: 'views/forgetpassword.html',
          controller: 'ForgetpasswordCtrl',
          controllerAs: 'forgetPassword'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.signOut,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //修改密码
    .state('root.modifyPassword', {
      url: '/modifyPassword',
      views: {
        'content' :{
          templateUrl: 'views/modifypassword.html',
          controller: 'ModifypasswordCtrl',
          controllerAs: 'modifyPassword'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.signIn,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //重置密码
    .state('root.resetPassword', {
      url: '/resetPassword',
      views: {
        'content' :{
          templateUrl: 'views/resetPassword.html',
          controller: 'ResetpasswordCtrl',
          controllerAs: 'resetPassword'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.signOut,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
    //moreReport
    .state('root.moreReport', {
      url: '/moreReport',
      views: {
        'content' :{
          templateUrl: 'views/morecontent.html',
          controller: 'moreReport'
        },
        'footer': {
          templateUrl: 'views/footer.html',
          controller: 'footer'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.public,
      onEnter: function($rootScope){
        $rootScope.footerShow = true;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
        $rootScope.routeHide = true;
      }
    })
    //阅读模式
    .state('root.reading', {
      url: '/reading',
      views: {
        'content' :{
          templateUrl: 'views/reading.html',
          controller: 'ReadingCtrl',
          controllerAs: 'reading'
        }
      },
      ACCESS_LEVEL: ACCESS_LEVEL.public,
      onEnter: function($rootScope){
        $rootScope.footerShow = false;
        $rootScope.rightButShow = false;
        $rootScope.mobShow = false;
      }
    })
});

angular.module('webApp')
  .run(function ($rootScope,$location,$http,CacheFactory,popBox,$ionicScrollDelegate,cookie,$timeout,jqLite) {
    $rootScope.$on('$stateChangeSuccess',function(){
      /*
       * 智能机UC浏览器bug:
       *
       */
      if(navigator.userAgent.indexOf('UCBrowser') > -1) {
        $timeout(function(){
          for(var i = 0;i < jqLite.$('signInBox').length;i++){
            jqLite.$('signInBox').eq(i).addClass('fixed');
          }
        },1000);
      }
    });
    //安全令牌
    $rootScope.$on('$stateChangeStart',function(evt,next){
      if(next.ACCESS_LEVEL == 2 && !cookie.getCookie('access_token')){
        evt.preventDefault();
        popBox.showConfirm('警告','请先登录！');
      }
      else if(next.ACCESS_LEVEL == 1 && cookie.getCookie('access_token')){
        evt.preventDefault();
        popBox.showConfirm('警告','无效的页面路由请求！');
      }
      else if(
        (next.ACCESS_LEVEL == 0) ||
        (next.ACCESS_LEVEL == 2 && cookie.getCookie('access_token')) ||
        (next.ACCESS_LEVEL == 1 && !cookie.getCookie('access_token'))
      ){
        $ionicScrollDelegate.scrollTop();
      }
      else{
        evt.preventDefault();
        $location.path('/root/home');
        popBox.showConfirm('警告','网页安全警告！');
      }
    });
    //URL闪存
    $http.defaults.cache = CacheFactory('myCache', {
      capacity: 5, //最大缓存数量
      maxAge: 60 * 1000, //新插入的缓存过期时间
      cacheFlushInterval: 60 * 1000, //所有缓存过期时间
      deleteOnExpire: 'aggressive', //删除过期缓存
      storageMode: 'memory' //缓存写入内存
    });
});

